package sng.modules.model
{
	import mx.collections.ArrayCollection;
	
	import org.casalib.events.LoadEvent;
	import org.casalib.load.DataLoad;
	import org.casalib.util.DateUtil;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import sng.data.info.BuildingInfo;
	import sng.data.info.GlobalInfo;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.HudVO;
	import sng.modules.model.vo.SeedVO;
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.system.NameSpace;
	import sng.modules.system.SocialSystem;
	import sng.utils.ArrayCollectionUtil;
	import sng.utils.BuildingUtil;
	import sng.utils.TimeUtil;

	/**
	 * 
	 * @author leonliu
	 * 
	 */	
	public class HudProxy extends Proxy
	{
		public function HudProxy()
		{
			super(NameSpace.HudProxyName);
		}
		
		protected var _gamedataLoad:DataLoad;
		protected var _playerdataLoad:DataLoad;
		private var _ibuildingType:int=0;
		private var _playerXML:XML;
		
		private var _vo:HudVO;
		
		private var _iEvent:int=0;//事件类型 0载入自己数据，1 载入好友数据
		
		
		/**
		 *1.载入游戏配置数据填充到hudvo 
		 * 
		 */		
		public function LoadGameXML(path:String):void {
			
			
			_gamedataLoad = new DataLoad(path);
			_gamedataLoad.addEventListener(LoadEvent.COMPLETE, _onGameComplete);
			_gamedataLoad.start();
		}
		
		 
		protected function _onGameComplete(e:LoadEvent):void {
			
			var _gameXML:XML=this._gamedataLoad.dataAsXml;
			//BuildingInfo.dataXML = _gameXML;//静态类赋值  leonliu注释
			//静态调用
//			var bvo:BuildingVO = BuildingInfo.getBuildingVOFromTemplateId(3);
			_vo.buildinglist.removeAll();
			_vo.seedlist.removeAll();
			var _buildingVO:BuildingVO;
			for each(var _building:XML in _gameXML.tbbuilddefs.tbbuilddef)
			{
				_buildingVO=new BuildingVO();
				_buildingVO.buildingid=_building.@id;
				_buildingVO.buildingTypeId = _building.@typeid;
				_buildingVO.tabid = _building.@tabid;
				_buildingVO.buildingname=_building.@name;
				_buildingVO.buildingprice=_building.@price;
				_buildingVO.buildingimg=_building.@headimgurl;
				_buildingVO.buildingSwfUrl=_building.@swfUrl;
				_buildingVO.createStep=_building.@createStep;
				_buildingVO.stragegy=_building.@stragegy;
				_buildingVO.populationDownLimit = _building.@populationDownLimit;
				_buildingVO.populationUpLimit = _building.@populationUpLimit;
				_buildingVO.populationUpTimes = _building.@populationUpTimes;
				_buildingVO.countdown = _building.@countdown;				
				_buildingVO.cols = _building.@cols;				
				_buildingVO.rows = _building.@rows;		
				_buildingVO.populationbuildlimit = _building.@populationBuildLimit;
				_buildingVO.continuingCreate = (_building.@ continuingCreate==1?true:false);
				_buildingVO.bonusRate = _building.@bonusRate;
				_buildingVO.width = _building.@width;
				_buildingVO.height = _building.@height;
				_buildingVO.dropCoin = _building.@dropCoin;
				_buildingVO.dropGoods = _building.@dropGoods;
				_buildingVO.dropExp = _building.@dropExp;
				_buildingVO.needGoods = _building.@needGoods;
				
				_vo.buildinglist.addItem(_buildingVO);

			}
			
			for each(var _seed:XML in _gameXML.tbseeddefs.tbseed)
			{
				//<tbseed id="1" name="花生" maturetime="200" price="200" harvestgoods="100"  swfurl="assets/flash/b11.swf" mouseimg=""></tbseed>
				var _seedVO:SeedVO=new SeedVO();
				_seedVO.id=_seed.@id;
				_seedVO.tabid=_seed.@tabid;
				_seedVO.name=_seed.@name;
				_seedVO.maturetime=_seed.@maturetime;
				_seedVO.price=_seed.@price;
				_seedVO.harvestgoods=_seed.@harvestgoods;
				_seedVO.swfurl=_seed.@swfurl;
				_seedVO.mouseimg=_seed.@mouseimg;
				_seedVO.headimgurl=_seed.@headimgurl;
				_seedVO.dropGoods=_seed.@dropGoods;
				_seedVO.dropCoin=_seed.@dropCoin;
				
				_vo.seedlist.addItem(_seedVO);
				//农田类型为8  将种子的列表也加载到建筑列表中，种子列表的ID从1000开始
				
				_buildingVO=new BuildingVO();
				_buildingVO.buildingid=_seedVO.id;
				_buildingVO.tabid=_seedVO.tabid;
				_buildingVO.buildingTypeId = BuildingUtil.FARM_TYPE;
				_buildingVO.buildingname=_seedVO.name;
				_buildingVO.buildingprice=_seedVO.price;
				_buildingVO.buildingimg=_seedVO.headimgurl;
				_buildingVO.dropGoods=_seedVO.dropGoods;
				_buildingVO.dropCoin=_seedVO.dropCoin;
				_vo.buildinglist.addItem(_buildingVO);
			}
			
			
			BuildingInfo.buildinglist= ArrayCollectionUtil.clone(_vo.buildinglist);
			BuildingInfo.seedlist=_vo.seedlist;
		}
		
		/**
		 * 2.载入用户xml数据填充到hudvo
		 * 
		 */		
		public function LoadPlayerXML(path:String):void {
			
			 _playerdataLoad = new DataLoad(path);
			_playerdataLoad.addEventListener(LoadEvent.COMPLETE, _onPlayerComplete);
			_playerdataLoad.start();
		}
		
		protected function _onPlayerComplete(e:LoadEvent):void {
			
			_playerXML=_playerdataLoad.dataAsXml;
			var i:int=int(_playerXML.tbglobal.totalpersonnum);
			
			_vo.mainvo.usernick=_playerXML.@nick;
			_vo.mainvo.uid=_playerXML.@uid;
			_vo.mainvo.level=_playerXML.@level;
			
			_vo.mainvo.money=_playerXML.tbglobal.money;
			_vo.mainvo.totalpersonnum=_playerXML.tbglobal.totalpersonnum;//总人口数
			_vo.mainvo.personnum=_playerXML.tbglobal.personnum;//当前人口数
			_vo.mainvo.satisfaction=_playerXML.tbglobal.satisfaction;//满意度
			_vo.mainvo.energy=_playerXML.tbglobal.energy;//神力值
			_vo.mainvo.goods=_playerXML.tbglobal.goods;//货物
			_vo.mainvo.goodsuplimit=_playerXML.tbglobal.goodsuplimit;//货物总容量
			
			
			
			GlobalInfo.mainvo=_vo.mainvo;//*****************************给全局属性赋值
			
			_vo.userbuildinglist.removeAll();
			for each(var _building:XML in _playerXML.tbbuilds.tbbuild)
			{
				
				var _buildingVO:UserBuildingVO=new UserBuildingVO();
				_buildingVO.uid=_building.@uid;
				_buildingVO.buildingid = _building.@buildingid;
				_buildingVO.templateId = _building.@templateId;
				_buildingVO.x=_building.@x;
				_buildingVO.y=_building.@y;
				_buildingVO.countdown=_building.@countdown;
				_buildingVO.countdownTime=Number(_building.@countdown)+TimeUtil.getTimeSecond();
				_buildingVO.currentState=_building.@currentState;
				_buildingVO.nextState=_building.@nextState;
				_buildingVO.buildStep=_building.@buildStep;
				_buildingVO.direction=_building.@direction;
				_buildingVO.population = _building.@population;
				_buildingVO.populationUpTimes = _building.@populationUpTimes;
				_buildingVO.para1 = _building.@para1;
				var s:String=_building.@isIdle;
				_buildingVO.isIdle=(s=="true");
				_buildingVO.totalBonusRate = _building.@totalBonusRate;
				_vo.userbuildinglist.addItem(_buildingVO);
				
			}
			
			//3.通知视图进行更新
			if (_iEvent==0)
				sendNotification(NameSpace.HudInitComplete, _vo);		
			else
				sendNotification(SocialSystem.SocialFriendsCityEntry, _vo);	
		}
		

		/**
		 * 
		 * @param vo 界面数据模型
		 * 功能：初始化界面数据模型
		 * 
		 */		
		public function InitGameData(vo:HudVO):void
		{
			_vo=vo;
			_iEvent=0;
			LoadGameXML("conf/game.xml");
			LoadPlayerXML("conf/player.xml");
			// todo by kevin 该处存在问题，网络原因会造成载入顺序问题
		}
		

		
		/**
		 * 
		 * @param vo 
		 * 功能：初始化好友城市数据
		 * 
		 */		
		public function initFriendData(uid:uint):void
		{
			_vo=new HudVO();
			_iEvent=1;
//			LoadGameXML("conf/game.xml");// by kevin 临时添加，切换好友城市的hudVO没有游戏配置数据。
			LoadPlayerXML("conf/friend.xml");
		}
		
	}
}